package main

import (
	"encoding/gob"
	"flag"
	"log"
	"luiz/crawler/engine"
	"luiz/crawler/model"
	"luiz/crawler/scheduler"
	"luiz/crawler/ziru/parser"
	"luiz/crawler_distributed/config"
	"luiz/crawler_distributed/persist/client"
	"luiz/crawler_distributed/prcsupport"
	client2 "luiz/crawler_distributed/worker/client"
	"net/rpc"
	"strings"
)

var (
	itemSaverHost = flag.String("it", "", "it host")
	workHost      = flag.String("work", "", "work host")
)

func main() {
	flag.Parse()
	gob.Register(model.House{})
	saver, _ := client.ItemSaver(*itemSaverHost)

	pool := createClientPool(strings.Split(*workHost, ","))
	processor := client2.CreateProcessor(pool)
	c := &engine.ConcurrentEngine{Scheduler: &scheduler.SimpleScheduler{},
		WorkerCount: 10, ItemChan: saver, RequestProcessor: processor}

	c.Run(engine.Request{"http://gz.ziroom.com/z/z1/?isOpen=0", engine.NewFuncParser(parser.ParseCityList, config.ParseCityList)})
}
func createClientPool(host []string) chan *rpc.Client {
	var clients []*rpc.Client
	for _, h := range host {
		cli, err := prcsupport.NewCli(h)
		if err == nil {
			clients = append(clients, cli)
			log.Printf("conn to %s", h)
		} else {
			log.Printf("error conn to %s :%v", h, err)
		}

	}
	c := make(chan *rpc.Client)
	go func() {
		for {
			for _, r := range clients {
				c <- r
			}
		}
	}()
	return c
}
